package com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.dto.AsphaltMatchNameListDto;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.dto.AsphaltMatchNameSelectDto;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.entity.AsphaltMatchEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.entity.AsphaltMatchNameEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.entity.AsphaltMatchTypeEntity;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.mapper.AsphaltMatchNameMapper;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchNameService;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchService;
import com.qzsoft.zhjd.asphalt.modules.asphaltmatch.service.AsphaltMatchTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;

/**
 * @ClassName AsphaltMatchNameServiceImpl
 * @Description 配方名称业务接口实现
 * @author linhuawei
 * @since 2021-11-22
 */
@Service
public class AsphaltMatchNameServiceImpl extends ServiceImpl<AsphaltMatchNameMapper, AsphaltMatchNameEntity>
        implements AsphaltMatchNameService {

    @Autowired
    private AsphaltMatchTypeService asphaltMatchTypeService;

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Autowired
    private AsphaltMatchService asphaltMatchService;

    @Override
    public IPage<AsphaltMatchNameListDto> pageDto(Map<String, Object> params) {
        IPage<AsphaltMatchNameListDto> page = baseServiceComponent.getPage(params, AsphaltMatchNameListDto.class);
        return baseMapper.queryDto(page, params);
    }

    @Override
    public List<AsphaltMatchNameSelectDto> listSelect(Map<String, Object> params) {
        return baseMapper.listSelect(params);
    }

    @Override
    public AsphaltMatchNameEntity getByName(String name) {
        QueryWrapper<AsphaltMatchNameEntity> queryWrapper = new QueryWrapper<AsphaltMatchNameEntity>();
        queryWrapper.eq("name", name);
        List<AsphaltMatchNameEntity> list = baseMapper.selectList(queryWrapper);
        return null == list || 0 == list.size() ? null : list.get(0);
    }

    @Override
    public boolean save(AsphaltMatchNameEntity entity) {
        // 判断类型是否存在
        AsphaltMatchTypeEntity typeEntity = asphaltMatchTypeService.getByType(entity.getType());
        if (null == typeEntity) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "级配类型[" + entity.getType() + "]不存在");
        }

        // 判断配方名称是否存在
        QueryWrapper<AsphaltMatchNameEntity> queryWrapper = new QueryWrapper<AsphaltMatchNameEntity>();
        queryWrapper.eq("name", entity.getName());
        Integer count = baseMapper.selectCount(queryWrapper);
        if (null != count && 0 < count) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "配方名称[" + entity.getName() + "]已经存在");
        }

        // 执行添加操作
        return super.save(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        // 获取当前配方名称
        AsphaltMatchNameEntity entity = baseMapper.selectById(id);
        if(null == entity) {
            return true;
        }

        // 判断配方名称是否在用
        QueryWrapper<AsphaltMatchEntity> queryWrapper = new QueryWrapper<AsphaltMatchEntity>();
        queryWrapper.eq("match_name", entity.getName());
        Integer count = asphaltMatchService.count(queryWrapper);
        if(null != count && 0 < count) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "当前配方名称正在使用，不可删除");
        }

        return super.removeById(id);
    }

}
